package cn.quevo.cube.api.define;

import cn.quevo.common.annotation.RequiresPermissions;
import cn.quevo.common.entity.ResultEntity;
import cn.quevo.common.enums.ResultStatusEnum;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.core.spring.SpringController;
import cn.quevo.cube.provide.define.entity.CubeColumnItem;
import cn.quevo.cube.provide.define.entity.query.CubeColumnItemQuery;
import cn.quevo.cube.provide.define.service.CubeColumnItemService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 字段选择项 RestController
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/11 14:56:48
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
@RestController("cubeColumnItemController")
@RequestMapping("/cube/")
public class CubeColumnItemRestController extends SpringController{

    @Resource(name="cubeColumnItemService")
    private CubeColumnItemService cubeColumnItemService;

    /**
     * 分页加载字段选择项
     *
     * @param cubeColumnItemQuery
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:56:48
     */
    @RequestMapping(value="cubeColumnItem",method= RequestMethod.GET)
    @RequiresPermissions("cube:cubeColumnItem:list")
    public ResultEntity page(CubeColumnItemQuery cubeColumnItemQuery){
        return cubeColumnItemService.getPage(cubeColumnItemQuery);
    }

    /**
     * 保存字段选择项
     *
     * @param cubeColumnItem
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:56:48
     */
    @RequestMapping(value="cubeColumnItem",method=RequestMethod.POST)
    @RequiresPermissions("cube:cubeColumnItem:create")
    public ResultEntity create(CubeColumnItem cubeColumnItem){
        if(cubeColumnItemService.create(cubeColumnItem) <= 0){
            return new ResultEntity(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }

    /**
     * 修改字段选择项
     *
     * @param cubeColumnItem
     * @param id
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:56:48
     */
    @RequestMapping(value="cubeColumnItem//{id:\\d+}",method=RequestMethod.PUT)
    @RequiresPermissions("cube:cubeColumnItem:update")
    public ResultEntity update(CubeColumnItem cubeColumnItem, @PathVariable("id") Long id){
        if (StringUtils.isBlank(id)) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_UNSELECTED);
        }
        cubeColumnItem.setId(id);
        if (StringUtils.isBlank(this.cubeColumnItemService.update(cubeColumnItem))) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }
}
